//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Audio/Resources/AudioSample.h"
#include "Audio/Loaders/AudioLoader.h"

#if defined(OP_AUDIO_IN_SEPARATE_THREAD)

namespace Engine
{
namespace Audio
{


	//
	// Audio Engine
	//

	class AudioEngine : public BaseObject
	{
	// variables
	private:
		ParallelThread	_audioThread;
		AudioLoader		_audioLoader;
		bool			_initialized;


	// methods
	public:
		AudioEngine (const EngineSubSystemsRef ess) :
			BaseObject(ess), _initialized(false)
		{
			ESS()->SetAudioEngine( this );
		}


		~AudioEngine ()
		{
			Ptr< ResourceManager > rm	= ESS()->GetResourceManager();

			if ( rm.IsNotNull() )
			{
				_audioLoader.Unregister( rm );
			}

			ESS()->SetAudioEngine( null );
		}


		bool Init ()
		{
			if ( _initialized )
			{
				WARNING( "reinitialization!" );
				return true;
			}

			_initialized = BaseAudioEngine::Init();
			return _initialized;
		}


		void InitResources ()
		{
			Ptr< ResourceManager > rm	= ESS()->GetResourceManager();

			CHECK_ERR( rm.IsNotNull(), );

			_audioLoader.Register( rm );
		}

	private:
		bool _Init ()
		{
			if ( _initialized )
			{
				WARNING( "reinitialization!" );
				return true;
			}

			_initialized = BaseAudioEngine::Init();
			return _initialized;
		}


		void _InitResources ()
		{
			Ptr< ResourceManager > rm	= ESS()->GetResourceManager();

			CHECK_ERR( rm.IsNotNull(), );

			_audioLoader.Register( rm );
		}
	};


}	// Audio
}	// Engine

#endif	// OP_AUDIO_IN_SEPARATE_THREAD